<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>编辑部门</title>
  <link rel="stylesheet" href="../css/edit_department.css"> <!-- 假设对应的样式表 -->
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="container">
  <h1>编辑部门</h1>
  <form id="editDepartmentForm">
    <input type="hidden" id="departmentId" name="departmentid"> <!-- 隐藏域存放部门ID -->
    <label for="departmentName">部门名称：</label>
    <input type="text" id="departmentName" name="departname" required><br>

    <label for="leaderId">部门领导ID：</label>
    <input type="text" id="leaderId" name="leaderid" required><br>

    <label for="phone">联系电话：</label>
    <input type="text" id="phone" name="phone" required><br>

    <label for="location">位置：</label>
    <input type="text" id="location" name="location" required><br>

    <button type="submit" class="button">保存修改</button>
  </form>
</div>
<script>
  $(document).ready(function () {
    const urlParams = new URLSearchParams(window.location.search);
    const departmentId = urlParams.get("id");
    if (departmentId) {
      // 根据部门ID获取部门详细信息
      $.ajax({
        url: "/queryDepartmentByID", // 获取部门详细信息接口
        method: "GET",
        data: { dId: departmentId },
        success: function (department) {
          $('#departmentId').val(department.departmentid);
          $('#departmentName').val(department.departname);
          $('#leaderId').val(department.leaderid);
          $('#phone').val(department.phone);
          $('#location').val(department.location);
        },
        error: function () {
          alert("获取部门信息失败，请检查网络或服务端。");
        }
      });
    }
    $('#editDepartmentForm').submit(function (e) {
      e.preventDefault();
      const departmentData = {
        departmentid: $('#departmentId').val(),
        departname: $('#departmentName').val(),
        leaderid: $('#leaderId').val(),
        phone: $('#phone').val(),
        location: $('#location').val()
      };
      $.ajax({
        url: "/updateDepartment",
        method: "POST",
        contentType: "application/json",
        data: JSON.stringify(departmentData),
        success: function (res) {
          if (res > 0) {
            alert("部门信息修改成功！");
            window.location.href = "department.html"; // 返回部门管理页面刷新数据
          } else {
            alert("部门信息修改失败！");
          }
        },
        error: function () {
          alert("部门信息修改操作失败，请检查网络或服务端。");
        }
      });
    });
  });
</script>
</body>
</html>